package com.eascs.dubbo.api.internal;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.LoanInfoEdtiService;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.form.InternalEditForm;
import com.eascs.admin.o2o.internal.form.LoanapplyForm;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.service.PayMentService;
import com.eascs.admin.o2o.internal.util.BeanUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.utils.BankRate;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;

@Service
public class LoanInfoEdtiServiceImpl implements LoanInfoEdtiService {
	private static Logger logger = LoggerFactory.getLogger(LoanInfoEdtiServiceImpl.class);

	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Autowired
	private PayMentService payMentService;

	@Override
	public O2oLoanapplyEntity queryO2oLoanapplyEntity(String loanapplyNo) {
		return o2oLoanapplyService.findById(loanapplyNo);
	}

	@Override
	public LoanapplyExpand queryLoanapplyExpand(String loanapplyNo) {
		return loanapplyExpandService.queryLoanapplyNo(loanapplyNo);
	}

	@Override
	public String saveInternalInfo(InternalEditForm internalEditForm) {
		LoanapplyForm loanapplyForm = new LoanapplyForm();
		BeanUtils.copyProperties(internalEditForm, loanapplyForm);
		if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_BC)) {
			loanapplyForm.setLoanrate(BankRate.BANK_BC_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CBC)) {
			loanapplyForm.setLoanrate(BankRate.BANK_CBC_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_GACZ)) {
			loanapplyForm.setLoanrate(BankRate.BANK_GACZ_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CCBHN)) {
			loanapplyForm.setLoanrate(BankRate.BANK_CCBHN_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_QLB)) {
			loanapplyForm.setLoanrate(BankRate.BANK_QLB_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CIBJN)) {
			loanapplyForm.setLoanrate(BankRate.BANK_CIBJN_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_JCB)) {
			loanapplyForm.setLoanrate(BankRate.BANK_JCB_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_ZZB)) {
			loanapplyForm.setLoanrate(BankRate.BANK_ZZB_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SPDBGZ)) {
			loanapplyForm.setLoanrate(BankRate.BANK_SPDBGZ_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SXB)) {
			loanapplyForm.setLoanrate(BankRate.BANK_SXB_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_ZLB)) {
			loanapplyForm.setLoanrate(BankRate.BANK_ZLB_RATE);
		} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SPDBJN)) {
			loanapplyForm.setLoanrate(BankRate.BANK_SPDBJN_RATE);
		} else {
			loanapplyForm.setLoanrate(BankRate.BANK_OTH_RATE);
		}
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(loanapplyForm.getLoanapplyNo());
		// 计算保证金、管理费、综合汇率、风险补偿金
		if(loanapply.getGeneralRate()==null){
			loanapplyForm.setGeneralRate(new BigDecimal(0.15));
		}else{
			loanapplyForm.setGeneralRate(loanapply.getGeneralRate());
		}
		loanapplyForm.setManagefeerate(loanapply.getManagefeerate());
		loanapplyForm.setDepositrate(loanapply.getDepositrate());
		BigDecimal risk=(loanapplyForm.getGeneralRate().subtract(loanapplyForm.getLoanrate()).subtract(loanapply.getManagefeerate())).setScale(4, RoundingMode.HALF_EVEN);
		loanapplyForm.setRisk(risk);
		logger.info("内审新建编辑贷款信息,loanapplyForm:{}", loanapplyForm.toString());
		if (loanapplyForm.getRisk().doubleValue() < 0) {
			return "风险补偿金不能小于0";
		}
		if (loanapplyForm.getEaMount().compareTo(new BigDecimal(1000000.00)) == 1) {
			return "EA批复金额不能大于100万";
		}
		/*if (StringUtil.isBlank(loanapplyForm.getRepayMentway())) {
			return "还款方式不能为空";
		}
		if (StringUtil.isBlank(loanapplyForm.getReferrerName())) {
			return "内部推荐人不能为空";
		}*/
		if (StatusContexts.LOAN_TYPE_BK.equals(loanapply.getLoanType())
				|| StatusContexts.LOAN_TYPE_BP.equals(loanapply.getLoanType())
				|| StatusContexts.LOAN_TYPE_BP_JX.equals(loanapply.getLoanType())
				|| StatusContexts.LOAN_TYPE_SG.equals(loanapply.getLoanType())
				|| StatusContexts.LOAN_TYPE_YC.equals(loanapply.getLoanType())
				|| StatusContexts.LOAN_TYPE_SC.equals(loanapply.getLoanType())) {
			payMentService.bKloan_ns(loanapplyForm, loanapply);
		} else if (StatusContexts.LOAN_TYPE_EA.equals(loanapply.getLoanType())) {
			payMentService.eAloan_ns(loanapplyForm, loanapply);
		} else {
			payMentService.fQloan_ns(loanapplyForm, loanapply);
		}
		return null;
	}
}
